package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Novel;
import com.ruoyi.system.domain.Vo.CommentVo;
import com.ruoyi.system.service.INovelService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Comment;
import com.ruoyi.system.service.ICommentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小说评论Controller
 * 
 * @author 许雅萍
 * @date 2024-11-16
 */
@Controller
@RequestMapping("/system/comment")
public class CommentController extends BaseController
{
    private String prefix = "system/comment";

    @Autowired
    private ICommentService commentService;

    @Autowired
    private INovelService novelService;

    @Autowired
    private ISysUserService userService;

    @GetMapping()
    public String comment(ModelMap mmap)
    {
        mmap.put("userId", ShiroUtils.getUserId());
        return prefix + "/comment";
    }

    @PostMapping("/reply")
    @ResponseBody
    public AjaxResult reply(Long commentId, String content) {
        // 实现回复逻辑
        return success();
    }


    /**
     * 查询小说评论列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Comment comment)
    {
        startPage();
        List<Comment> list = commentService.selectCommentList(comment);
        List<CommentVo> commentVos = new ArrayList<>();
        list.forEach(c ->{
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(c,commentVo);

            SysUser sysUser = userService.selectUserById(c.getUserId());
            Novel novel = novelService.selectNovelByNovelId(c.getNovelId());

            commentVo.setNovelName(novel.getTitle());
            commentVo.setUserName(sysUser.getUserName());
            if (c.getUserId().equals(ShiroUtils.getUserId()) || ShiroUtils.getSysUser().isAdmin()){
                commentVo.setShowRemove(true);
            }else {
                commentVo.setShowRemove(false);
            }
            commentVo.setIsAdmin(ShiroUtils.getSysUser().isAdmin());
            commentVos.add(commentVo);
        });
        return getDataTable(commentVos);
    }

    /**
     * 导出小说评论列表
     */

    @Log(title = "小说评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Comment comment)
    {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        return util.exportExcel(list, "小说评论数据");
    }

    /**
     * 新增小说评论
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<Novel> novels = novelService.selectNovelList(new Novel());
        modelMap.put("novels",novels);
        return prefix + "/add";
    }

    /**
     * 新增保存小说评论
     */

    @Log(title = "小说评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Comment comment)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Long userId = sysUser.getUserId();

        comment.setUserId(userId);
        return toAjax(commentService.insertComment(comment));
    }

    /**
     * 修改小说评论
     */

    @GetMapping("/edit/{commentId}")
    public String edit(@PathVariable("commentId") Long commentId, ModelMap mmap)
    {
        Comment comment = commentService.selectCommentByCommentId(commentId);
        mmap.put("comment", comment);

        List<Novel> novels = novelService.selectNovelList(new Novel());
        mmap.put("novels",novels);
        return prefix + "/edit";
    }

    /**
     * 修改保存小说评论
     */

    @Log(title = "小说评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Comment comment)
    {
        return toAjax(commentService.updateComment(comment));
    }

    /**
     * 删除小说评论
     */

    @Log(title = "小说评论", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(commentService.deleteCommentByCommentIds(ids));
    }
}
